import store from '../vuex/store'
import { getOS, getBrowser } from '../utils/utils'
import { allApi } from '../api/api'

export default class RouterConfig {
    constructor(router) {
        this.router = router
    }
    render() {
        const router = this.router
        router.beforeEach((to, from, next) => {
            if(getOS() == 'Windows' || getOS() == 'Mac OS'){
                if(to.name != 'not-mobile' ){
                    next({ path: '/not-mobile' })
                    return
                }
            }
            //document.title = to.meta.title

            allApi.log({
                e_n:'page_v',
                e_v:{
                    value:to.meta.title
                }
            })

            let routerList = store.state.localInfo.routerList
            let isRouterFirst = routerList.length == 0
            let toIndex = _.indexOf(routerList,to.name)
            let fromIndex = _.indexOf(routerList,from.name)
            // console.log(toIndex)
            // console.log(fromIndex)
            if(toIndex == -1 && fromIndex == -1){
                store.dispatch('setRouterAnimation','router')
                store.dispatch('setRouterList',{
                    name:to.name
                })
                next()
                return
            }
            if(toIndex == -1 || toIndex > fromIndex){
                store.dispatch('setRouterAnimation','router-in')
                store.dispatch('setRouterList',{
                    name:to.name
                })
            }
            if(toIndex != -1 && toIndex == fromIndex){
                store.dispatch('setRouterAnimation','router-in')
            }
            if(toIndex != -1 && toIndex < fromIndex){
                store.dispatch('setRouterAnimation','router-out')
                store.dispatch('setRouterList',{
                    name:to.name,
                    index:fromIndex
                })
            }
            // let isRouterLast = to.name == routerList[routerList.length-2]

            // if(from.name == undefined){
            //     store.dispatch('setRouterList',to.name)
            //     next()
            //     return
            // }

            // if(isRouterFirst || routerList.length > 1 && isRouterLast){
            //     if(isRouterFirst){
            //         store.dispatch('setRouterAnimation','router-in')
            //         store.dispatch('setRouterList',to.name)
            //     }
            //     else{
            //         store.dispatch('setRouterAnimation','router-out')
            //         store.dispatch('setRouterList','removeLastRouter')
            //     }
            // }
            // else{
            //     store.dispatch('setRouterAnimation','router-in')
            //     store.dispatch('setRouterList',to.name)
            // }
            next()

        })
    }
}
